<?php

declare(strict_types=1);

namespace App\Controller\playlet;
use App\Controller\BaseController;
use App\Model\BaseBanner;
use App\Model\PlayletBlocksRanking;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Common\Response\SystemCode;
use App\Service\playlet\BaseBannerService;

#[AutoController]
class BaseBannerController  extends BaseController
{

    #[Inject]
    private BaseBannerService $baseBannerService;
    /**
     * banner列表
     */
    public function Index(RequestInterface $request)
    {

        $data = $this->baseBannerService->search($request->query());

        return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$data);

    }
    /**
     * banner创建
     */
    public function Create(RequestInterface $request){

        $result = $this->baseBannerService->createOrUpdate($request->post());

        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '创建成功');
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

        }
    }

    /**
     * banner更新
     */
    public function Update(int $id,RequestInterface $request){

        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{
            $baseBannerInfo = BaseBanner::query()->select(['id','cover','name','status','sort'])->where('id',$id)->first();
            if(empty($baseBannerInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, 'banner不存在');
            }
            if($request->getMethod() == 'GET'){
                return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$baseBannerInfo);

            }else{
                $result = $this->baseBannerService->createOrUpdate($request->post(),$baseBannerInfo);

                if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                    return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
                }else{
                    return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

                }

            }
        }
    }

    /**
     * banner删除
     */
    public function Delete(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $baseBannerInfo = BaseBanner::query()->where('id', $id)->first();
            if (empty($baseBannerInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, 'banner不存在');
            }
            $baseBannerInfo->deleted_at = date('Y-m-d H:i:s');
            $baseBannerInfo->save();
            return $this->response(SystemCode::SYSTEM_SUCCESS, '操作成功');
        }
    }
}